package com.controller.admin;

import com.controller.BaseController;
import com.entity.NewsCatalog;
import com.github.pagehelper.PageInfo;
import com.service.imple.admin.NewsCatalogServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻分类
 */

@Controller
@RequestMapping("admin/NewsCatalog")
public class AdminNewsCatalogController extends BaseController<NewsCatalogServiceImple> {

    @RequestMapping("toNewsCatalog")
    public String toNewsCatalog(){
        return "center/admin/newsClassify/NewsClassify";
    }

    @RequestMapping("NewsCatalog")
    public String NewsCatalog(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum, ModelMap modelMap,String name){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("name",name);
        PageInfo<NewsCatalog> pageInfo = BaseService.selectByPage(map);
        modelMap.put("pageInfo",pageInfo);
        return "center/admin/newsClassify/NewsClassifyPage";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        BaseService.delete(id);
        return "center/admin/newsClassify/NewsClassify";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<NewsCatalog> list= BaseService.select(map);
        modelMap.put("list",list);
        return "center/admin/newsClassify/index";
    }

    @RequestMapping("Update")
    @ResponseBody
    public Map<String, Object> toUpdate(NewsCatalog newsCatalog){
        int count = BaseService.update(newsCatalog);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd( ){
        return "center/admin/newsClassify/addNewsClassify";
    }

    @RequestMapping("Add")
    @ResponseBody
    public Map<String,Object> toAdd(NewsCatalog newsCatalog){
        int count = BaseService.insert(newsCatalog);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

}
